﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.项目成果管理.在制品管理.service;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.项目成果管理.service;
using DXApplicationTangche.UC.项目成果管理.在制品管理.model;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.生产计划.生产批生产.model;
using DXApplicationTangche.UC.全部工序.service;
using DXApplicationTangche.UC.货号管理;

namespace DXApplicationTangche.UC.项目成果管理.在制品管理
{
    public partial class FormVI质检 : DevExpress.XtraEditors.XtraForm
    {
        private String code;
        private String batchNo { get; set; }//批号
        private String barcode { get; set; }//水洗标
        private QcItemsDTO DTO质检 = new QcItemsDTO();
        private OrderCommitDTO返回 orderCommitDTO返回 = new OrderCommitDTO返回();
        private List<InspectSizeDTO> list半成品量尺 = new List<InspectSizeDTO>();
        private List<DTO半成品量尺呈现> list半成品量尺呈现 = new List<DTO半成品量尺呈现>();
        private DTO项目成果提交 DTO项目成果提交 = new DTO项目成果提交();
        private List<DesignItemCheckDTO> list半成品设计点 = new List<DesignItemCheckDTO>();
        private List<DTO半成品设计点呈现> list半成品设计点呈现 = new List<DTO半成品设计点呈现>();
        private XtraUserControl工序显示设计 uc工序显示设计;//工序显示设计
        public FormVI质检(String code)
        {
            InitializeComponent();
            this.code = code;
            if ("01.0005".Equals(this.code) || "02".Equals(this.code))
            {
                this.xtraTabPage工艺设计.PageVisible = false;
            }
            this.uc工序显示设计 = new XtraUserControl工序显示设计();
            this.uc工序显示设计.Dock = DockStyle.Fill;
            this.xtraTabPage工艺设计.Controls.Clear();
            this.xtraTabPage工艺设计.Controls.Add(this.uc工序显示设计);
            if ("01.0005".Equals(this.code) || "01.0007".Equals(this.code) || "01.0005.01".Equals(this.code))
            {
                this.扫生产批号.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                this.layoutControlItem4.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                this.ActiveControl = this.textBox扫描水洗标;
            }
            if ("01.0005.01".Equals(this.code))
            {
                this.treeList半成品量尺.OptionsBehavior.Editable = false;
                this.gridView半成品设计点.OptionsBehavior.Editable = false;
            }
        }

        private void textBox扫码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.textBox扫描生产批号.Text.Trim()))
                {
                    return;
                }
                this.batchNo = this.textBox扫描生产批号.Text.Trim();
                this.barcode = null;
                try
                {
                    this.DTO质检 = 在制品管理Service.getQcItemsByBatchNo(this.batchNo);
                    this.orderCommitDTO返回 = 订单预览新Service.get订单预览By批号(this.batchNo);
                }
                catch
                {
                    MessageBox.Show("无查询信息");
                }
                this.显示();
                this.刷新显示半成品量尺();
                this.刷新显示半成品设计点();
                
            }
        }
        private void textBox扫描水洗标_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrEmpty(this.textBox扫描水洗标.Text.Trim()))
                {
                    return;
                }
                this.barcode = this.textBox扫描水洗标.Text.Trim();
                try
                {
                    this.DTO质检 = 在制品管理Service.getQcItemsByBarcode(this.barcode);
                    if (String.IsNullOrEmpty(this.DTO质检.batchNo))
                    {
                        MessageBox.Show("未查询到之间信息");
                    }
                    else
                    {
                        this.batchNo = this.DTO质检.batchNo;
                        this.orderCommitDTO返回 = 订单预览新Service.get订单预览By批号(this.batchNo);
                    }
                    
                }
                catch
                {
                    MessageBox.Show("无查询信息");
                }
                this.显示();
                this.刷新显示半成品量尺();
                this.刷新显示半成品设计点();

            }
        }
        private void 显示()
        {
            this.list半成品量尺 = this.DTO质检.inspectSizeDTOS;
            this.list半成品设计点 = this.DTO质检.designItemCheckDTOS;
            this.richTextBox备注.Text = this.DTO质检.remarks;
            this.textBox款式.Text = this.DTO质检.styleNameCn;
            try { this.textBox尺码.Text = this.DTO质检.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { this.textBox尺码.Text = ""; }
            this.textBox面料名称.Text = this.DTO质检.materialNameCn;
            this.textBox面料编码.Text = this.DTO质检.materialCode;
            this.pictureBox款式.LoadAsync(URL.图片url + URL.款式图片路径 + this.DTO质检.styleImagePath);
            this.pictureBox面料.LoadAsync(URL.图片url + URL.面料图片路径 + this.DTO质检.materialImagePath);
            this.textBox订单时间.Text = Convert.ToDateTime(this.orderCommitDTO返回.orderDate).ToString();
            this.textBox客户名.Text = this.orderCommitDTO返回.customerName;
            this.textBox订单号.Text = this.orderCommitDTO返回.orderNo;
            this.uc工序显示设计.refreahData(this.code, this.barcode, this.orderCommitDTO返回.styleNameCn, false);
        }

        /// <summary>
        /// 检查尺寸通过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void repositoryItemCheckEdit1_Click(object sender, EventArgs e)
        {
            try
            {
                在制品管理Service.post半成品量尺检查(
                    this.orderCommitDTO返回.orderId,
                    this.treeList半成品量尺.FocusedNode["id"].ToString()
                    );
                //改变页面显示
                this.list半成品量尺
                    = new 在制品管理Model().build半成品量尺通过(
                        this.list半成品量尺,
                        this.treeList半成品量尺.FocusedNode["id"].ToString()
                        );
                this.刷新显示半成品量尺();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 检查设计点通过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void repositoryItemButtonEdit设计点_Click(object sender, EventArgs e)
        {
            try
            {
                在制品管理Service.post半成品设计点检查(
                    this.batchNo,
                    new 在制品管理Model().build取单行设计点(
                        this.list半成品设计点,
                        FunctionHelper.GetgridViewValue(
                            this.gridView半成品设计点,
                            this.gridView半成品设计点.FocusedRowHandle,
                            "itemCd"
                            ),
                        FunctionHelper.GetgridViewValue(
                            this.gridView半成品设计点,
                            this.gridView半成品设计点.FocusedRowHandle,
                            "itemValue"
                            )
                        )
                    );
                //改变页面显示
                this.list半成品设计点
                    = new 在制品管理Model().build半成品设计点通过(
                        this.list半成品设计点,
                        FunctionHelper.GetgridViewValue(
                            this.gridView半成品设计点,
                            this.gridView半成品设计点.FocusedRowHandle,
                            "itemCd"
                            ),
                        FunctionHelper.GetgridViewValue(
                            this.gridView半成品设计点,
                            this.gridView半成品设计点.FocusedRowHandle,
                            "itemValue"
                            )
                        );
                this.刷新显示半成品设计点();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void 刷新显示半成品量尺()
        {
            //转换显示
            this.list半成品量尺呈现 = new 在制品管理Model().build半成品量尺显示(this.list半成品量尺);
            this.treeList半成品量尺.DataSource = this.list半成品量尺呈现;
            this.treeList半成品量尺.ExpandAll();
        }
        private void 刷新显示半成品设计点()
        {
            //转换显示
            this.list半成品设计点呈现 = new 在制品管理Model().build半成品设计点显示(this.list半成品设计点);
            this.gridControl半成品设计点.DataSource = this.list半成品设计点呈现;
            this.gridView半成品设计点.RefreshData();
        }

        private void treeList半成品量尺_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        {
            if (e.Column.Name == "treeListColumnIfPass")
            {
                DTO半成品量尺呈现 row = this.treeList半成品量尺.GetDataRecordByNode(e.Node) as DTO半成品量尺呈现;
                if (row.ifPass.Equals("未通过"))
                {
                    e.Appearance.BackColor = Color.Red;
                    e.Appearance.ForeColor = Color.White;
                }
                else if (row.ifPass.Equals("通过"))
                {
                    e.Appearance.BackColor = Color.Green;
                    e.Appearance.ForeColor = Color.White;
                }
            }
        }

        private void gridView半成品设计点_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name == "gridColumnifPass")
            {
                if (e.CellValue.Equals("未通过"))
                {
                    e.Appearance.BackColor = Color.Red;
                    e.Appearance.ForeColor = Color.White;
                }
                else if (e.CellValue.Equals("通过"))
                {
                    e.Appearance.BackColor = Color.Green;
                    e.Appearance.ForeColor = Color.White;
                }
            }
        }

        private void simpleButton提交_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.batchNo)
                    || string.IsNullOrEmpty(this.code)
                        || string.IsNullOrEmpty(this.DTO质检.styleId)
                        )
            {
                MessageBox.Show("无扫描内容");
                return;
            }
            this.DTO项目成果提交 = new DTO项目成果提交(
                                        this.batchNo,
                                        this.code,
                                        FunctionHelper.JsonSerialization(
                                            new ProjectAchievementDTO(
                                                Const工序.qc,
                                                this.DTO质检
                                                )
                                            )
                                        );
            new FormVI质检提交(this.barcode,this.DTO项目成果提交).ShowDialog();
            //new Form项目成果提交(this.DTO项目成果提交).ShowDialog();
            this.textBox扫描生产批号.Clear();
            this.textBox扫描水洗标.Clear();
            this.textBox扫描生产批号.Focus();
            this.uc工序显示设计.clearData();
        }
        
        private void simpleButton清空生产批_Click(object sender, EventArgs e)
        {
            this.textBox扫描生产批号.Clear();
            this.ActiveControl = this.textBox扫描生产批号;
        }

        private void simpleButton清空水洗标_Click(object sender, EventArgs e)
        {
            this.textBox扫描水洗标.Clear();
            this.ActiveControl = this.textBox扫描水洗标;
        }
    }
}